package com.dwxt.sport.admin.service;

import com.dwxt.common.base.BaseResult;
import com.dwxt.common.base.Page;
import com.dwxt.common.base.StatusCode;
import com.dwxt.common.base.YesOrNo;
import com.dwxt.common.util.IDUtils;
import com.dwxt.sport.business.entity.DictValue;
import com.dwxt.sport.business.service.DictValueService;
import com.dwxt.sport.dao.WifeDeviceMapper;
import com.dwxt.sport.entity.WifeDevice;
import com.dwxt.sport.util.ShuWeiUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DeviceServiceAdmin {
    @Autowired
    private WifeDeviceMapper wifeDeviceMapper;

    @Autowired
    private DictValueService dictValueService;

    public BaseResult selectDevicePage(Page page, WifeDevice wifeDevice) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        Example example = new Example(WifeDevice.class);
        example.orderBy("createTime").desc();
        Example.Criteria criteria = example.createCriteria();
        if (wifeDevice.getModelNumber() != null) {
            criteria.andEqualTo("modelNumber", wifeDevice.getModelNumber());
        }
        if (wifeDevice.getName() != null) {
            criteria.andEqualTo("name", wifeDevice.getName());
        }
        if (wifeDevice.getLocation() != null) {
            criteria.andEqualTo("location", wifeDevice.getLocation());
        }
        if (wifeDevice.getType() != null) {
            criteria.andEqualTo("type", wifeDevice.getType());
        }
        if (wifeDevice.getStatus() != null) {
            criteria.andEqualTo("status", wifeDevice.getStatus());
        }

        List<WifeDevice> wifeDeviceList = wifeDeviceMapper.selectByExample(example);
        /*List<WifeDevice> collect = wifeDeviceList.stream().sorted(Comparator.comparing(WifeDevice::getCreateTime).reversed()).collect(Collectors.toList());*/
        PageInfo<WifeDevice> pageInfo = new PageInfo<>(wifeDeviceList);
        return BaseResult.ok(pageInfo);
    }

    public BaseResult createDevice(WifeDevice wifeDevice) {
        if (wifeDevice.getType() == 1) {
            List<WifeDevice> wifeDeviceList = selectWifeDevicesByMac(wifeDevice.getMacAddress());
            if (wifeDeviceList.size() > 0) {
                return BaseResult.build(StatusCode.MAC_ALREADY);
            } else {
                ShuWeiUtil.addBoxByMac(wifeDevice);
            }
        }
        wifeDevice.setId(IDUtils.genItemId());
        wifeDeviceMapper.insertSelective(wifeDevice);
        return BaseResult.ok();
    }

    private List<WifeDevice> selectWifeDevicesByMac(String mac) {
        Example example = new Example(WifeDevice.class);
        example.createCriteria().andEqualTo("macAddress", mac);
        return wifeDeviceMapper.selectByExample(example);
    }

    public BaseResult updateDevice(WifeDevice wifeDevice) {
        WifeDevice selectByPrimaryKey = wifeDeviceMapper.selectByPrimaryKey(wifeDevice.getId());
        if (wifeDevice.getMacAddress().equalsIgnoreCase(selectByPrimaryKey.getMacAddress())) {
            wifeDeviceMapper.updateByPrimaryKeySelective(wifeDevice);
            ShuWeiUtil.updateBox(wifeDevice);
        } else {
            if (wifeDevice.getType() == 1) {
                int size = selectWifeDevicesByMac(wifeDevice.getMacAddress()).size();
                if (size <= 0) {
                    wifeDeviceMapper.updateByPrimaryKeySelective(wifeDevice);
                    ShuWeiUtil.addBoxByMac(wifeDevice);
                } else {
                    return BaseResult.build(StatusCode.MAC_ALREADY);
                }
            } else {
                wifeDeviceMapper.updateByPrimaryKeySelective(wifeDevice);
            }
        }
        return BaseResult.ok();
    }

    public void deleteDevice(String id) {
        WifeDevice wifeDevice = wifeDeviceMapper.selectByPrimaryKey(id);
        if (wifeDevice.getType() == 1 && selectWifeDevicesByMac(wifeDevice.getMacAddress()).size() <= 1) {
            ShuWeiUtil.deleteBox(wifeDevice.getMacAddress());
        }
        wifeDeviceMapper.deleteByPrimaryKey(id);
    }

    public void upOrDown(String id) {
        WifeDevice wifeDevice = wifeDeviceMapper.selectByPrimaryKey(id);
        Integer isActive = wifeDevice.getIsActive() == YesOrNo.YES ? YesOrNo.NO : YesOrNo.YES;
        wifeDevice.setIsActive(isActive.byteValue());
        wifeDeviceMapper.updateByPrimaryKeySelective(wifeDevice);
    }

    public BaseResult selectDeviceGroup() {
        List<Map> maps = wifeDeviceMapper.selectDeviceGroup();
        return BaseResult.ok(maps);
    }

    public BaseResult selectDeviceType() {
        List<DictValue> wifiDeviceType = dictValueService.selectDictByCode("wifiDeviceType");
        return BaseResult.ok(wifiDeviceType);
    }

    public BaseResult selectDeviceModelByType(Integer type) {
        List<DictValue> wifiDeviceType = dictValueService.selectChildDictListByParent("wifiDeviceType", type + "");
        return BaseResult.ok(wifiDeviceType);
    }

    public List<WifeDevice> selectDeviceByIds(List<String> deviceIds) {
        if (deviceIds.size() == 0) {
            return new ArrayList<>();
        }
        Example example = new Example(WifeDevice.class);
        example.createCriteria().andIn("id", deviceIds);
        return wifeDeviceMapper.selectByExample(example);
    }

}
